//判读链表是否有循环的部分
#include <iostream>

struct ListNode
{
    int val = 0;
    struct ListNode* next = nullptr;
};

class Solution {
public:
    bool hasCycle(ListNode *head) {
        if(head == nullptr)
            return false;

        //双指针
        ListNode* slow = head;
        ListNode* fast = head;
        //这样做判断可以避免 --- 只有一个元素但不是循环链表，slow fast同时走到nullptr，判断为true的情况
        while(fast && fast->next)
        {
            slow = slow->next;
            fast = fast->next->next;

            if(slow == fast)
                return true;
        }
        return false;
    }
};